package cn.zsb.zsb.service;

import cn.zsb.zsb.dao.SuserMapper;
import cn.zsb.zsb.entity.Suser;
import cn.zsb.zsb.entity.SuserExample;
import cn.zsb.zsb.util.StrUtil;
import cn.zsb.zsb.vo.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author oldliu
 * @since 1.0
 */
@Service

public class SuserService {
    @Autowired
    private SuserMapper mapper;


    public int add(Suser user) {
        //sadminMapper.deleteByPrimaryKey(5L);
        return mapper.insertSelective(user);
    }

    public Page<Suser> search(String phone, String sname, String school, String major, int page, int size) {

        com.github.pagehelper.Page<Suser> p = PageHelper.startPage(page, size);

        SuserExample user = new SuserExample();
        SuserExample.Criteria criteria = user.createCriteria();
        if (!StrUtil.isBlank(sname))
            criteria.andSnameLike("%" + sname + "%");

        if (!StrUtil.isBlank(phone))
            criteria.andPhoneLike("%" + phone + "%");
        if (!StrUtil.isBlank(school))
            criteria.andSchoolLike("%" + school + "%");
        if (!StrUtil.isBlank(major))
            criteria.andMajorLike("%" + major + "%");

        List<Suser> susers = mapper.selectByExample(user);

        //long total = mapper.countByExample(user);
        //return new Page<Suser>(p.getTotal(),susers);
        return new Page<Suser>(p.getTotal(), page, size, susers);
    }


    public Suser get(Long id) {
        return mapper.selectByPrimaryKey(id);

    }

    public int batDel(long[] id) {

        int d = 0;

        for (long i : id)
            d += mapper.deleteByPrimaryKey(i);

        return d;
    }

    @Transactional(readOnly = false)
    public int update(Suser user) {
        return this.mapper.updateByPrimaryKeySelective(user);
    }

    @Transactional(readOnly = false)
    public int resetPass(Suser user) {
        return this.mapper.updatePass(user);
    }
}
